{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c671b822",
   "metadata": {},
   "source": [
    "## 1. 格式化输出\n",
    "python字符串格式化符号:\n",
    "* %c 格式化字符及其ASCII码 \n",
    "* %s 格式化字符串 \n",
    "* %d 格式化整数 \n",
    "* %u 格式化无符号整型 \n",
    "* %o 格式化无符号八进制数 \n",
    "* %x 格式化无符号十六进制数 \n",
    "* %X 格式化无符号十六进制数（大写）\n",
    "* %f 格式化浮点数字，可指定小数点后的精度 \n",
    "* %e 用科学计数法格式化浮点数 \n",
    "* %E 作用同%e，用科学计数法格式化浮点数 \n",
    "* %g %f 和 %e 的简写 \n",
    "* %G %f 和 %E 的简写 \n",
    "* %p 用十六进制数格式化变量的地址  \n",
    "其他：\"平面向量({},{})\".format(self.x, self.y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "515213ba",
   "metadata": {},
   "source": [
    "格式化操作符辅助指令:\n",
    "\n",
    "* m.n.  m 是显示的最小总宽度（如果指定的话）,n 是小数点后的位数(如果指定的话)\n",
    "* *定义宽度或者小数点精度\n",
    "* -用做左对齐\n",
    "* +在正数前面显示加号( + )\n",
    "* 在正数前面显示空格#\n",
    "* 在八进制数前面显示零('0')\n",
    "* 在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')\n",
    "* 0显示的数字前面填充'0'而不是默认的空格%\n",
    "* '%%'输出一个单一的'%'\n",
    "* (var)映射变量(字典参数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3df889cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "print (\"I like %s and can eat %.2f kg.\" % (\"orange\", 1.5))\n",
    "# %9.3f 9位，3位小数, 前面是5位整数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c967cce2",
   "metadata": {},
   "source": [
    "## 2 format\n",
    "### 2.1 参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb4af6b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小红今年18岁，住在北京\n",
      "数学的数学成绩是95分\n"
     ]
    }
   ],
   "source": [
    "# 多个参数\n",
    "text = \"{}今年{}岁，住在{}\".format(\"小红\", 18, \"北京\")\n",
    "print(text)  # 输出：小红今年18岁，住在北京\n",
    "\n",
    "# 重复使用同一个参数\n",
    "text = \"{0}的{0}成绩是{1}分\".format(\"数学\", 95)\n",
    "print(text)  # 输出：数学的数学成绩是95分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "579524e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三今年25岁，是工程师\n"
     ]
    }
   ],
   "source": [
    "# 使用关键字参数\n",
    "text = \"{name}今年{age}岁，是{job}\".format(name=\"张三\", age=25, job=\"工程师\")\n",
    "print(text)  # 输出：张三今年25岁，是工程师"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58b317b6",
   "metadata": {},
   "source": [
    "### 2.2 数字格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c416b404",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "π的值是：3.14\n"
     ]
    }
   ],
   "source": [
    "# 浮点数精度控制\n",
    "pi = 3.1415926\n",
    "print(\"π的值是：{:.2f}\".format(pi)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0bfff26d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正确率：85.7%\n"
     ]
    }
   ],
   "source": [
    "# 百分比显示\n",
    "score = 0.8567\n",
    "print(\"正确率：{:.1%}\".format(score))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9b79ef83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号：00042\n",
      "编号：   42\n"
     ]
    }
   ],
   "source": [
    "# 整数填充\n",
    "number = 42\n",
    "print(\"编号：{:05d}\".format(number))       \n",
    "print(\"编号：{:5d}\".format(number))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56d1ee21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "金额：123,456,789\n"
     ]
    }
   ],
   "source": [
    "# 千位分隔符\n",
    "big_number = 123456789\n",
    "print(\"金额：{:,}\".format(big_number))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7940c002",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制：255\n",
      "二进制：11111111\n"
     ]
    }
   ],
   "source": [
    "# 不同进制显示\n",
    "num = 255\n",
    "print(\"十进制：{}\".format(num))           \n",
    "print(\"二进制：{:b}\".format(num)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e37976c",
   "metadata": {},
   "source": [
    "### 2.3 对齐和宽度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eb24d08e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|    Python|\n",
      "|Python    |\n",
      "|  Python  |\n"
     ]
    }
   ],
   "source": [
    "text = \"Python\"\n",
    "print(\"|{:>10}|\".format(text))    # 右对齐：|    Python|\n",
    "print(\"|{:<10}|\".format(text))    # 左对齐：|Python    |\n",
    "print(\"|{:^10}|\".format(text))    # 居中对齐：|  Python  |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d7d8197a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|****Python|\n"
     ]
    }
   ],
   "source": [
    "# 填充字符\n",
    "print(\"|{:*>10}|\".format(text))   # 输出：|****Python|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0753746c",
   "metadata": {},
   "source": [
    "### 2.4 高级特性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "df32a9d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 访问列表\n",
    "fruits = [\"苹果\", \"香蕉\", \"橙子\"]\n",
    "text = \"我喜欢吃{0[0]}和{0[2]}\".format(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70598d70",
   "metadata": {},
   "source": [
    "## 3. f-string "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0133cda8",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"小明\"\n",
    "age = 20\n",
    "\n",
    "# f-string 方法\n",
    "text2 = f\"{name}今年{age}岁\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10d7cee7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
